/**
 * @file    sys_file.h
 *          
 * @author  Li.Hua <lihua_338@163.com>.
 *          
 * @date    2014-04-30
 *          
 * @brief   
 *          
 * @note    
 *          
 * Modification history
 * ----------------------------------------------------------------------------
 * Date         Version  Author       History
 * ----------------------------------------------------------------------------
 *
 */
#ifndef _SYS_FILE_H_
#define _SYS_FILE_H_

#include <stdint.h>


#include <sys/types.h>


#ifndef loff_t
typedef signed long     loff_t;
#endif


#ifndef off_t
typedef signed long		off_t;
#endif


struct utimbuf {
  long actime;
  long modtime; 
};

struct file_stat {
    long    size;
    long    mode;
    long    atime;
    long    mtime;
    long    ctime;
    long    blksize;
    long    blocks;
    char    name[32];
};

#define	    MODE_IFMASK	    0170000	    /* type of file mask*/

#define		MODE_IFDIR      0040000	    /* directory */
#define		MODE_IFCHR      0020000	    /* character special */
#define		MODE_IFBLK      0060000	    /* block special */
#define		MODE_IFREG      0100000	    /* regular */
#define		MODE_IFLNK      0120000	    /* symbolic link */
#define		MODE_IFSOCK     0140000	    /* socket */
#define		MODE_IFIFO      0010000	    /* fifo */

#define		MODE_IRUSR      0000400	    /* read permission, owner */
#define		MODE_IWUSR      0000200	    /* write permission, owner */
#define		MODE_IXUSR      0000100     /* execute/search permission, owner */

#define		MODE_IRGRP      0000040	    /* read permission, group */
#define		MODE_IWGRP      0000020	    /* write permission, grougroup */
#define		MODE_IXGRP      0000010     /* execute/search permission, group */

#define		MODE_IROTH      0000004	    /* read permission, other */
#define		MODE_IWOTH      0000002	    /* write permission, other */
#define		MODE_IXOTH      0000001     /* execute/search permission, other */


struct fs_stat {
    long    f_type;     /* type of file system (see below) */
    long    f_bsize;    /* optimal transfer block size */
    long    f_blocks;   /* total data blocks in file system */
    long    f_bfree;    /* free blocks in fs */
    long    f_bavail;   /* free blocks avail to non-superuser */
    long    f_files;    /* total file nodes in file system */
    long    f_ffree;    /* free file nodes in fs */
    long    f_fsid;     /* file system id */
    long    f_namelen;  /* maximum length of filenames */
};

/* File system types: */

#define     ADFS_SUPER_MAGIC        0xadf5
#define     AFFS_SUPER_MAGIC        0xADFF
#define     BEFS_SUPER_MAGIC        0x42465331
#define     BFS_MAGIC               0x1BADFACE
#define     CIFS_MAGIC_NUMBER       0xFF534D42
#define     CODA_SUPER_MAGIC        0x73757245
#define     COH_SUPER_MAGIC         0x012FF7B7
#define     CRAMFS_MAGIC            0x28cd3d45
#define     DEVFS_SUPER_MAGIC       0x1373
#define     EFS_SUPER_MAGIC         0x00414A53
#define     EXT_SUPER_MAGIC         0x137D
#define     EXT2_OLD_SUPER_MAGIC    0xEF51
#define     EXT2_SUPER_MAGIC        0xEF53
#define     EXT3_SUPER_MAGIC        0xEF53
#define     HFS_SUPER_MAGIC         0x4244
#define     HPFS_SUPER_MAGIC        0xF995E849
#define     HUGETLBFS_MAGIC         0x958458f6
#define     ISOFS_SUPER_MAGIC       0x9660
#define     JFFS2_SUPER_MAGIC       0x72b6
#define     JFS_SUPER_MAGIC         0x3153464a
#define     MINIX_SUPER_MAGIC       0x137F /* orig. minix */
#define     MINIX_SUPER_MAGIC2      0x138F /* 30 char minix */
#define     MINIX2_SUPER_MAGIC      0x2468 /* minix V2 */
#define     MINIX2_SUPER_MAGIC2     0x2478 /* minix V2, 30 char names */
#define     MSDOS_SUPER_MAGIC       0x4d44
#define     NCP_SUPER_MAGIC         0x564c
#define     NFS_SUPER_MAGIC         0x6969
#define     NTFS_SB_MAGIC           0x5346544e
#define     OPENPROM_SUPER_MAGIC    0x9fa1
#define     PROC_SUPER_MAGIC        0x9fa0
#define     QNX4_SUPER_MAGIC        0x002f
#define     REISERFS_SUPER_MAGIC    0x52654973
#define     ROMFS_MAGIC             0x7275
#define     SMB_SUPER_MAGIC         0x517B
#define     SYSV2_SUPER_MAGIC       0x012FF7B6
#define     SYSV4_SUPER_MAGIC       0x012FF7B5
#define     TMPFS_MAGIC             0x01021994
#define     UDF_SUPER_MAGIC         0x15013346
#define     UFS_MAGIC               0x00011954
#define     USBDEVICE_SUPER_MAGIC   0x9fa2
#define     VXFS_SUPER_MAGIC        0xa501FCF5
#define     XENIX_SUPER_MAGIC       0x012FF7B4
#define     XFS_SUPER_MAGIC         0x58465342
#define     _XIAFS_SUPER_MAGIC      0x012FD16D

typedef     void (*wake_up_func_t)(int in_isr);


#define IOC_SETFL		0xFFFFFFFD
#define IOC_GETFL		0xFFFFFFFE

/* Definition of open flags in GCC and ARMCC is not the same  */
#define OF_RDONLY	    00000000
#define OF_WRONLY	    00000001
#define OF_RDWR		    00000002
#define OF_APPEND	    00000010
#define OF_FASYNC		00000100
#define OF_CREAT		00001000
#define OF_TRUNC		00002000
#define OF_EXCL		    00004000
#define OF_SYNC		    00020000
#define OF_NONBLOCK	    00040000
#define OF_NOCTTY	    00100000


#ifndef O_RDONLY
#define O_RDONLY	OF_RDONLY	
#endif
#ifndef O_WRONLY
#define O_WRONLY	OF_WRONLY	
#endif
#ifndef O_RDWR
#define O_RDWR		OF_RDWR		
#endif
#ifndef O_APPEND
#define O_APPEND	OF_APPEND	
#endif
#ifndef O_FASYNC
#define O_FASYNC	OF_FASYNC	
#endif
#ifndef O_CREAT
#define O_CREAT		OF_CREAT	
#endif
#ifndef O_TRUNC
#define O_TRUNC		OF_TRUNC	
#endif
#ifndef O_EXCL
#define O_EXCL		OF_EXCL		
#endif
#ifndef O_SYNC
#define O_SYNC		OF_SYNC		
#endif
#ifndef O_NONBLOCK
#define O_NONBLOCK	OF_NONBLOCK
#endif
#ifndef O_NOCTTY
#define O_NOCTTY	OF_NOCTTY	
#endif

#ifndef F_OK
#define	R_OK	4		/* Test for read permission.  */
#define	W_OK	2		/* Test for write permission.  */
#define	X_OK	1		/* Test for execute permission.  */
#define	F_OK	0		/* Test for existence.  */
#endif

int sys_open(const char *filename, uint32_t flags, uint32_t mode);
ssize_t sys_read(int fd, void *buf, size_t count);
ssize_t sys_write(int fd, const void *buf, size_t count);
loff_t sys_lseek(int fd, loff_t offset, int origin);
int sys_ftruncate(int fd, off_t length);
int sys_fsync(int fd);

int sys_ioctl(int fd, unsigned int cmd, unsigned long arg);
int sys_ioctl2(int fd, unsigned int cmd, unsigned long arg, unsigned long arg1);
int sys_close(int fd);
int sys_fstat(int fd, struct file_stat *buf);
int poll_set(int fd, wake_up_func_t wake_up);

int sys_exit(int val);

int sys_opendir(const char *pathname);

int sys_mkdir(const char *pathname);
int sys_unlink(const char *filename);
int sys_rename(const char *path_old, const char* path_new);
int sys_stat(const char *path, struct file_stat *fno);
int sys_access(const char *path, int mode);
int sys_fs_access(const char *path, int mode);
int sys_chmod(const char *path, int value, int mask);
int sys_chdir(const char *path);
int sys_getcwd(char *path, int len);
int sys_statfs(struct fs_stat *st);
int sys_utime(const char *path, struct utimbuf *fno);
int sys_close_all(void);

void svc_sys_exit(int ret);

int sys_resume(void);
int sys_suspend(void);

#endif /*_SYS_FILE_H_ */



